﻿package _9_Menu;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * 2011-6-7 16:45:55
 * @author Administrator
 * 演示菜单热键及快捷键的使用
 */
public class TJMenu {
    JFrame frame;
    Container contentPane;
    JMenuBar menuBar;
    JMenu fileMenu,editMenu,helpMenu;
    JMenuItem fmOpen,fmClose,fmNew;
    JMenuItem emUndo,emCut,emCopy,emFind;
    JLabel label;
    public TJMenu(){
        frame=new JFrame("菜单测试");
        contentPane=frame.getContentPane();
        setMenu();
        contentPane.add(menuBar,BorderLayout.NORTH);
        label=new JLabel("没有操作");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setPreferredSize(new Dimension(400,400));
        contentPane.add(label,BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void setMenu(){
        menuBar=new JMenuBar();
        fileMenu=new JMenu("文件(F)");
        fileMenu.setMnemonic('f');
        editMenu=new JMenu("编辑(E)");
        editMenu.setMnemonic('e');
        helpMenu=new JMenu("帮助(H)");
        helpMenu.setMnemonic('h');
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        fmOpen=new JMenuItem("打开(o)", 'o');
        fmOpen.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("打开");
            }
        });
        fmOpen.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK));
        fmClose=new JMenuItem("关闭(c)",'c');
        fmClose.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("关闭");
            }
        });
        fmNew=new JMenuItem("新建(n)",'n');
        fmNew.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("新建");
            }
        });
        fileMenu.add(fmOpen);
        fileMenu.add(fmNew);
        fileMenu.add(fmClose);
        emFind=new JMenuItem("查找(F)",'f');
        emFind.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("查找");
            }
        });
        emCut=new JMenuItem("剪切(T)",'t');
        emCut.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("剪切");
            }
        });
        emCopy=new JMenuItem("复制(C)",'c');
        emCopy.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                label.setText("复制");
            }
        });
        editMenu.add(emFind);
        editMenu.add(emCopy);
        editMenu.add(emCut);
    }
    public static void main(String args[]){
        new TJMenu();
    }
}
